//
//  CartoonVerticalTableViewCell.m
//  WuLi
//
//  Created by Gollum on 16/9/23.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "CartoonVerticalTableViewCell.h"
#import "CartoonHorizontalCollectionViewCell.h"

//新作推荐
#import "CartoonData.h"
#import "CartoonDetailData.h"
#import "CartoonModel.h"
#import "NetworkTool.h"
#import "UIImageView+WebCache.h"

//刚刚更新
#import "CartoonUpdateModel.h"
#import "CartoonUpdateData.h"
#import "CartoonUpdateDetailData.h"

//爽快向
#import "FrankCartoonModel.h"
#import "FrankCartoonData.h"
#import "FrankCartoonDetailData.h"

//女生向
#import "CartoonGirlData.h"
#import "CartoonGirlDetailData.h"
#import "CartoonGirlModel.h"

//日漫
#import "CartoonJapanData.h"
#import "CartoonJapanDetailData.h"
#import "CartoonJapanModel.h"

//韩漫
#import "CartoonKoreaData.h"
#import "CartoonKoreaDetailData.h"
#import "CartoonKoreaModel.h"

#import "CartoonViewController.h"
#import "CartoonDetailTagViewController.h"
#import "CartoonDetailLIistViewController.h"

#define kCartoonVerticalCollectionViewCellReuse @"CartoonVerticalReuse"
#define kCartoonVerticalCollectionViewCellReuse1 @"CartoonVerticalReuse1"
#define kCartoonVerticalCollectionViewCellReuse2 @"CartoonVerticalReuse2"
#define kCartoonVerticalCollectionViewCellReuse3 @"CartoonVerticalReuse3"
#define kCartoonVerticalCollectionViewCellReuse4 @"CartoonVerticalReuse4"
#define kCartoonVerticalCollectionViewCellReuse5 @"CartoonVerticalReuse5"
#define kCartoonVerticalCollectionViewCellReuse6 @"CartoonVerticalReuse6"

@interface CartoonVerticalTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>
//新作推荐
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)CartoonModel *cartoonModel;
//刚刚更新
@property (nonatomic,strong)NSMutableArray *updateData;
@property (nonatomic,strong)CartoonUpdateModel *updateModel;
//爽快向
@property (nonatomic,strong)NSMutableArray *frankData;
@property (nonatomic,strong)FrankCartoonModel *frankModel;

//女生向
@property (nonatomic,strong)NSMutableArray *girlData;
@property (nonatomic,strong)CartoonGirlModel *girlModel;

//日漫
@property (nonatomic,strong)NSMutableArray *japanData;
@property (nonatomic,strong)CartoonJapanModel *japanModel;

//韩漫
@property (nonatomic,strong)NSMutableArray *koreaData;
@property (nonatomic,strong)CartoonKoreaModel *koreaModel;

@end


@implementation CartoonVerticalTableViewCell

- (void)awakeFromNib {
    
    // Initialization code
    _dataArray = [NSMutableArray array];
    _updateData = [NSMutableArray array];
    _frankData = [NSMutableArray array];
    _girlData = [NSMutableArray array];
    _japanData = [NSMutableArray array];
    _koreaData = [NSMutableArray array];
    [self handleData];
    [self handleDataUpdate];
    [self handleDataFrank];
    [self handleDataGirl];
    [self handleDataJapan];
    [self handleDataKorea];
    //布局
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    [self.collectionView setCollectionViewLayout:flowLayout];
    //注册（xib）
    
    
    UINib *nib = [UINib nibWithNibName:@"CartoonHorizontalCollectionViewCell" bundle:[NSBundle mainBundle]];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    _collectionView.scrollEnabled = NO;
    _collectionView.userInteractionEnabled = YES;
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse1];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse2];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse3];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse4];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse5];
   
}


- (void)handleData{
//新作推荐
    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_recommend&ui=0&ui_id=0&userid=0&type=20&start=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _cartoonModel = [CartoonModel modelWithDic:result];
        [_dataArray addObject:_cartoonModel];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)handleDataUpdate{

    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_recommend&ui=0&ui_id=0&userid=0&type=18&start=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _updateModel = [CartoonUpdateModel modelWithDic:result];
        [_updateData addObject:_updateModel];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];


}

- (void)handleDataFrank{
    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_recommend&ui=0&ui_id=0&userid=0&type=21&start=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _frankModel = [FrankCartoonModel modelWithDic:result];
        [_frankData addObject:_frankModel];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];


}
- (void)handleDataGirl{
    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_recommend&ui=0&ui_id=0&userid=0&type=28&start=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _girlModel = [CartoonGirlModel modelWithDic:result];
        [_frankData addObject:_girlModel];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];



}
- (void)handleDataJapan{
    
    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_recommend&ui=0&ui_id=0&userid=0&type=23&start=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _japanModel = [CartoonJapanModel modelWithDic:result];
        [_japanData addObject:_japanModel];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];



}
- (void)handleDataKorea{
    
    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRecommend&a=get_main_recommend&ui=0&ui_id=0&userid=0&type=27&start=0";
    
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _koreaModel = [CartoonKoreaModel modelWithDic:result];
        [_koreaData addObject:_koreaModel];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
    
}








- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    NSLog(@"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",self.nextResponder.nextResponder.nextResponder.nextResponder);

        return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    switch (_CartoonVerticalNumOfRow) {
            
        case 3:{
            
            CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse forIndexPath:indexPath];
            NSURL *url = [NSURL URLWithString:_cartoonModel.data.data[indexPath.row].thumb];
            [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
            cell.titleLabel.text = _cartoonModel.data.data[indexPath.row].title;
            return cell;
        }
            break;
            
            
            
        case 5:{
            CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse1 forIndexPath:indexPath];
            NSURL *url = [NSURL URLWithString:_updateModel.data.data[indexPath.row].thumb];
            [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
            cell.titleLabel.text = _updateModel.data.data[indexPath.row].title;
            return cell;
        
        }
            break;
            
        case 7:{
            CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse2 forIndexPath:indexPath];
            NSURL *url = [NSURL URLWithString:_frankModel.data.data[indexPath.row].thumb];
            [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
            cell.titleLabel.text = _frankModel.data.data[indexPath.row].title;
            return cell;
        
        }
            break;
            
        case 9:{
            CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse3 forIndexPath:indexPath];
            NSURL *url = [NSURL URLWithString:_girlModel.data.data[indexPath.row].thumb];
            [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
            cell.titleLabel.text = _girlModel.data.data[indexPath.row].title;
            return cell;
        
        }
            
            break;
        case 11:{
            CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse4 forIndexPath:indexPath];
            NSURL *url = [NSURL URLWithString:_japanModel.data.data[indexPath.row].thumb];
            [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
            cell.titleLabel.text = _japanModel.data.data[indexPath.row].title;
            return cell;
        
        }
            break;
            
        case 12:{
            CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse5 forIndexPath:indexPath];
            NSURL *url = [NSURL URLWithString:_koreaModel.data.data[indexPath.row].thumb];
            [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
            cell.titleLabel.text = _koreaModel.data.data[indexPath.row].title;
            return cell;
        
        }
            break;
        default:{
            
//            CartoonHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonVerticalCollectionViewCellReuse forIndexPath:indexPath];
//            NSURL *url = [NSURL URLWithString:_cartoonModel.data.data[indexPath.row].thumb];
//            [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
//            cell.titleLabel.text = _cartoonModel.data.data[indexPath.row].title;
//            return cell;
        }
            
            break;
    }
    
 
    return nil;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(self.contentView.frame.size.width / 3, [UIScreen mainScreen].bounds.size.width / 3 * 1.5);
    
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    
    switch (_CartoonVerticalNumOfRow) {
        case 3:{
            
//            if ([_cartoonModel.data.data[indexPath.row].view_type isEqualToString:@"0"]) {
//                CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
//                CartoonDetailTagViewController *tag = [[CartoonDetailTagViewController alloc] init];
//                tag.detailString = _cartoonModel.data.data[indexPath.row].descriptionla;
//                tag.imageUrl = _cartoonModel.data.data[indexPath.row].thumb;
//                tag.bookId = _cartoonModel.data.data[indexPath.row].bookid;
//                tag.catid = _cartoonModel.data.data[indexPath.row].catid;
//                tag.titleStr = _cartoonModel.data.data[indexPath.row].title;
//                tag.author = _cartoonModel.data.data[indexPath.row].author;
//                tag.updateTime = _cartoonModel.data.data[indexPath.row].updatetime;
//                [cattoon presentViewController:tag animated:YES completion:^{
//                    
//                }];
//
//            }else{
                CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
                CartoonDetailLIistViewController *list = [[CartoonDetailLIistViewController alloc] init];
                list.detailString = _cartoonModel.data.data[indexPath.row].descriptionla;
                list.imageUrl = _cartoonModel.data.data[indexPath.row].thumb;
                list.bookId = _cartoonModel.data.data[indexPath.row].bookid;
                list.catid = _cartoonModel.data.data[indexPath.row].catid;
                list.titleStr = _cartoonModel.data.data[indexPath.row].title;
                list.author = _cartoonModel.data.data[indexPath.row].author;
                list.updateTime = _cartoonModel.data.data[indexPath.row].updatetime;
                [cattoon presentViewController:list animated:YES completion:^{
                    
                }];
            
//            }
            
            
        }
            break;
        case 5:{
            
            if ([_updateModel.data.data[indexPath.row].view_type isEqualToString:@"0"]) {
                CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
                CartoonDetailTagViewController *tag = [[CartoonDetailTagViewController alloc] init];
                tag.detailString = _updateModel.data.data[indexPath.row].descriptionla;
                tag.imageUrl = _updateModel.data.data[indexPath.row].thumb;
                tag.bookId = _updateModel.data.data[indexPath.row].bookid;
                tag.catid = _updateModel.data.data[indexPath.row].catid;
                tag.titleStr = _updateModel.data.data[indexPath.row].title;
                tag.author = _updateModel.data.data[indexPath.row].author;
                tag.updateTime = _updateModel.data.data[indexPath.row].updatetime;
//                [cattoon presentViewController:tag animated:YES completion:^{
//                    
//                }];
                
            }else{
                CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
                CartoonDetailLIistViewController *list = [[CartoonDetailLIistViewController alloc] init];
                list.detailString = _updateModel.data.data[indexPath.row].thumb;
                list.imageUrl = _updateModel.data.data[indexPath.row].thumb;
                list.bookId = _updateModel.data.data[indexPath.row].bookid;
                list.catid = _updateModel.data.data[indexPath.row].catid;
                list.titleStr = _updateModel.data.data[indexPath.row].title;
                list.author = _updateModel.data.data[indexPath.row].author;
                list.updateTime = _updateModel.data.data[indexPath.row].updatetime;
//                [cattoon presentViewController:list animated:YES completion:^{
//                    
//                }];
                
            }
        
        }
        case 7:{
            CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
            CartoonDetailTagViewController *tag = [[CartoonDetailTagViewController alloc] init];
            tag.detailString = _japanModel.data.data[indexPath.row].thumb;
//            [cattoon presentViewController:tag animated:YES completion:^{
//                
//            }];
        }
        case 9:{
            CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
            CartoonDetailLIistViewController *list = [[CartoonDetailLIistViewController alloc] init];
            list.detailString = _cartoonModel.data.data[indexPath.row].thumb;
//            [cattoon presentViewController:list animated:YES completion:^{
//                
//            }];
        }
        case 11:{
            
            CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
            
            CartoonDetailTagViewController *tag = [[CartoonDetailTagViewController alloc] init];
            tag.detailString = _japanModel.data.data[indexPath.row].thumb;
//            [cattoon presentViewController:tag animated:YES completion:^{
//            }];
        }
        case 12:{
            CartoonViewController *cattoon = (CartoonViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
            CartoonDetailLIistViewController *list = [[CartoonDetailLIistViewController alloc] init];
            list.detailString = _cartoonModel.data.data[indexPath.row].thumb;
//            [cattoon presentViewController:list animated:YES completion:^{
//                
//            }];
        }
        default:
            break;
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
